package com.xckj.cycling.presenter;

import androidx.annotation.NonNull;

import com.xckj.cycling.bean.BaseBean;
import com.xckj.cycling.framework.net.RetrofitManager;
import com.xckj.cycling.global.Constant;
import com.xckj.cycling.iview.ISendCodeView;

import java.util.Objects;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @Author: ChenYao
 * @Describe: 发送验证码
 * @Date: 2023/3/6 23:56
 */
public class SendCodePresenter {
    private final ISendCodeView sendCodeView;

    public SendCodePresenter(ISendCodeView sendCodeView) {
        this.sendCodeView = sendCodeView;
    }

    /**
     * 发送验证码
     *
     * @param event 验证类型:register=注册,resetpwd=忘记密码
     * @param email 邮箱
     */
    public void sendCode(String event, String email) {
        sendCodeView.showLoading();
        Call<BaseBean<String>> call = RetrofitManager.getInstance().getService().sendCode(event, email);
        call.enqueue(new Callback<BaseBean<String>>() {
            @Override
            public void onResponse(@NonNull Call<BaseBean<String>> call, @NonNull Response<BaseBean<String>> response) {
                if (response.isSuccessful()) {
                    BaseBean<String> baseBean = response.body();
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        sendCodeView.sendCodeSuccess(baseBean.msg);
                    } else {
                        sendCodeView.sendCodeFailure(baseBean.msg);
                    }
                } else {
                    sendCodeView.sendCodeFailure("failed");
                }
                sendCodeView.hideDialog();
            }

            @Override
            public void onFailure(@NonNull Call<BaseBean<String>> call, @NonNull Throwable t) {
                t.printStackTrace();
                sendCodeView.sendCodeFailure("failed");
                sendCodeView.hideDialog();
            }
        });
    }
}
